package com.bjsxt.shop.orders.service.impl;

import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.bjsxt.shop.common.util.ConstatFinalUtil;
import com.bjsxt.shop.common.util.PageInfoUtil;
import com.bjsxt.shop.orders.dao.IAOrdersDao;
import com.bjsxt.shop.orders.dao.IAOrdersGoodsDao;
import com.bjsxt.shop.orders.dao.IAOrdersHistoryDao;
import com.bjsxt.shop.orders.pojo.AOrders;
import com.bjsxt.shop.orders.pojo.AOrdersGoods;
import com.bjsxt.shop.orders.pojo.AOrdersHistory;
import com.bjsxt.shop.orders.service.IOrdersService;

/**
 * 订单的Service类
 * @author wangshSxt
 *
 */
@Service("ordersService")
public class OrdersServiceImpl implements IOrdersService
{
	@Resource
	private IAOrdersDao ordersDao ; 
	@Resource
	private IAOrdersGoodsDao ordersGoodsDao;
	@Resource
	private IAOrdersHistoryDao ordersHistoryDao;
	
	@Override
	public AOrders findOneOrdersService(Map<String, Object> condMap)
	{
		return this.ordersDao.findOne(condMap);
	}
	
	@Override
	public JSONObject saveOneOrdersService(AOrders orders)
	{
		JSONObject resultJSON = new JSONObject();
		/* 影响条数 */
		int res = this.ordersDao.save(orders);
		/*String str = null ; 
		str.toString();*/
		if(res > 0 )
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
			
			/* 保存订单商品 */
			for (Iterator iterator = orders.getOrdersGoodList().iterator(); iterator.hasNext();)
			{
				AOrdersGoods goods = (AOrdersGoods) iterator.next();
				/* 设置订单Id */
				goods.setOrdersId(orders.getId());
				this.saveOneOrdersGoodsService(goods);
			}
			
			/* 叫保存订单操作明细表 */
			AOrdersHistory ordersHistory = new AOrdersHistory();
			ordersHistory.setOrdersId(orders.getId());
			ordersHistory.setSn("");
			ordersHistory.setContent("您提交订单");
			ordersHistory.setStatus(Byte.valueOf("1"));
			ordersHistory.setCreateTime(new Date());
			this.saveOneOrdersHistoryService(ordersHistory);
			
			/* 存储data */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", orders.getId() + "");
			dataJSON.put("effect", res + "");
			
			resultJSON.put("data", dataJSON);
		}else
		{
			resultJSON.put("code", "1");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("1"));
		}
		return resultJSON;
	}
	
	@Override
	public JSONObject updateOneOrdersService(AOrders orders)
	{
		JSONObject resultJSON = new JSONObject();
		/* 影响条数 */
		int res = this.ordersDao.update(orders);
		/*String str = null ; 
		str.toString();*/
		if(res > 0 )
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
			
			/* 存储data */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", orders.getId() + "");
			dataJSON.put("effect", res + "");
			
			resultJSON.put("data", dataJSON);
		}else
		{
			resultJSON.put("code", "2");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("2"));
		}
		return resultJSON;
	}

	@Override
	public JSONObject deleteOneOrdersService(AOrders orders)
	{
		JSONObject resultJSON = new JSONObject();
		/* 影响条数 */
		int res = this.ordersDao.delete(orders);
		/*String str = null ; 
		str.toString();*/
		if(res > 0 )
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
			
			/* 存储data */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", orders.getId() + "");
			dataJSON.put("effect", res + "");
			
			resultJSON.put("data", dataJSON);
		}else
		{
			resultJSON.put("code", "3");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("3"));
		}
		return resultJSON;
	}
	
	@Override
	public List<AOrders> findCondListOrdersService(PageInfoUtil pageInfoUtil,Map<String, Object> condMap)
	{
		/* 为关键字加上%,实现模糊匹配 
		 * */
		if(condMap.get("keyword") != null && !"".equalsIgnoreCase(condMap.get("keyword") + ""))
		{
			condMap.put("keyword", "%" + condMap.get("keyword")  + "%");
		}
		
		if(pageInfoUtil != null)
		{
			/* 分页 */
			/* 查询总记录数 */
			condMap.put("pageCount", "true");
			List<AOrders> roleList = this.ordersDao.findCondList(condMap);
			/* 取总记录数 */
			if(roleList.size() == 1)
			{
				pageInfoUtil.setTotalRecord(roleList.get(0).getId());
			}
			
			/* 查询数据 */
			condMap.put("pageCount", "false");
			/* limit ? , ? */
			condMap.put("currentRecord", pageInfoUtil.getCurrentRecord());
			condMap.put("pageSize", pageInfoUtil.getPageSize());
			return this.ordersDao.findCondList(condMap);
		}
		/* 不分页查询数据 */
		return this.ordersDao.findCondList(condMap);
	}
	
	@Override
	public AOrdersGoods findOneOrdersGoodsService(Map<String, Object> condMap)
	{
		return this.ordersGoodsDao.findOne(condMap);
	}
	
	@Override
	public JSONObject saveOneOrdersGoodsService(AOrdersGoods ordersGoods)
	{
		JSONObject resultJSON = new JSONObject();
		/* 影响条数 */
		int res = this.ordersGoodsDao.save(ordersGoods);
		/*String str = null ; 
		str.toString();*/
		if(res > 0 )
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
			
			/* 存储data */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", ordersGoods.getId() + "");
			dataJSON.put("effect", res + "");
			
			resultJSON.put("data", dataJSON);
		}else
		{
			resultJSON.put("code", "1");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("1"));
		}
		return resultJSON;
	}
	
	@Override
	public JSONObject updateOneOrdersGoodsService(AOrdersGoods ordersGoods)
	{
		JSONObject resultJSON = new JSONObject();
		/* 影响条数 */
		int res = this.ordersGoodsDao.update(ordersGoods);
		/*String str = null ; 
		str.toString();*/
		if(res > 0 )
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
			
			/* 存储data */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", ordersGoods.getId() + "");
			dataJSON.put("effect", res + "");
			
			resultJSON.put("data", dataJSON);
		}else
		{
			resultJSON.put("code", "2");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("2"));
		}
		return resultJSON;
	}

	@Override
	public JSONObject deleteOneOrdersGoodsService(AOrdersGoods ordersGoods)
	{
		JSONObject resultJSON = new JSONObject();
		/* 影响条数 */
		int res = this.ordersGoodsDao.delete(ordersGoods);
		/*String str = null ; 
		str.toString();*/
		if(res > 0 )
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
			
			/* 存储data */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", ordersGoods.getId() + "");
			dataJSON.put("effect", res + "");
			
			resultJSON.put("data", dataJSON);
		}else
		{
			resultJSON.put("code", "3");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("3"));
		}
		return resultJSON;
	}
	
	@Override
	public List<AOrdersGoods> findCondListOrdersGoodsService(PageInfoUtil pageInfoUtil,Map<String, Object> condMap)
	{
		/* 为关键字加上%,实现模糊匹配 
		 * */
		if(condMap.get("keyword") != null && !"".equalsIgnoreCase(condMap.get("keyword") + ""))
		{
			condMap.put("keyword", "%" + condMap.get("keyword")  + "%");
		}
		
		if(pageInfoUtil != null)
		{
			/* 分页 */
			/* 查询总记录数 */
			condMap.put("pageCount", "true");
			List<AOrdersGoods> roleList = this.ordersGoodsDao.findCondList(condMap);
			/* 取总记录数 */
			if(roleList.size() == 1)
			{
				pageInfoUtil.setTotalRecord(roleList.get(0).getId());
			}
			
			/* 查询数据 */
			condMap.put("pageCount", "false");
			/* limit ? , ? */
			condMap.put("currentRecord", pageInfoUtil.getCurrentRecord());
			condMap.put("pageSize", pageInfoUtil.getPageSize());
			return this.ordersGoodsDao.findCondList(condMap);
		}
		/* 不分页查询数据 */
		return this.ordersGoodsDao.findCondList(condMap);
	}
	
	@Override
	public AOrdersHistory findOneOrdersHistoryService(Map<String, Object> condMap)
	{
		return this.ordersHistoryDao.findOne(condMap);
	}
	
	@Override
	public JSONObject saveOneOrdersHistoryService(AOrdersHistory ordersHistory)
	{
		JSONObject resultJSON = new JSONObject();
		/* 影响条数 */
		int res = this.ordersHistoryDao.save(ordersHistory);
		/*String str = null ; 
		str.toString();*/
		if(res > 0 )
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
			
			/* 存储data */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", ordersHistory.getId() + "");
			dataJSON.put("effect", res + "");
			
			resultJSON.put("data", dataJSON);
		}else
		{
			resultJSON.put("code", "1");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("1"));
		}
		return resultJSON;
	}
	
	@Override
	public JSONObject updateOneOrdersHistoryService(AOrdersHistory ordersHistory)
	{
		JSONObject resultJSON = new JSONObject();
		/* 影响条数 */
		int res = this.ordersHistoryDao.update(ordersHistory);
		/*String str = null ; 
		str.toString();*/
		if(res > 0 )
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
			
			/* 存储data */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", ordersHistory.getId() + "");
			dataJSON.put("effect", res + "");
			
			resultJSON.put("data", dataJSON);
		}else
		{
			resultJSON.put("code", "2");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("2"));
		}
		return resultJSON;
	}

	@Override
	public JSONObject deleteOneOrdersHistoryService(AOrdersHistory ordersHistory)
	{
		JSONObject resultJSON = new JSONObject();
		/* 影响条数 */
		int res = this.ordersHistoryDao.delete(ordersHistory);
		/*String str = null ; 
		str.toString();*/
		if(res > 0 )
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
			
			/* 存储data */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", ordersHistory.getId() + "");
			dataJSON.put("effect", res + "");
			
			resultJSON.put("data", dataJSON);
		}else
		{
			resultJSON.put("code", "3");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("3"));
		}
		return resultJSON;
	}
	
	@Override
	public List<AOrdersHistory> findCondListOrdersHistoryService(PageInfoUtil pageInfoUtil,Map<String, Object> condMap)
	{
		/* 为关键字加上%,实现模糊匹配 
		 * */
		if(condMap.get("keyword") != null && !"".equalsIgnoreCase(condMap.get("keyword") + ""))
		{
			condMap.put("keyword", "%" + condMap.get("keyword")  + "%");
		}
		
		if(pageInfoUtil != null)
		{
			/* 分页 */
			/* 查询总记录数 */
			condMap.put("pageCount", "true");
			List<AOrdersHistory> roleList = this.ordersHistoryDao.findCondList(condMap);
			/* 取总记录数 */
			if(roleList.size() == 1)
			{
				pageInfoUtil.setTotalRecord(roleList.get(0).getId());
			}
			
			/* 查询数据 */
			condMap.put("pageCount", "false");
			/* limit ? , ? */
			condMap.put("currentRecord", pageInfoUtil.getCurrentRecord());
			condMap.put("pageSize", pageInfoUtil.getPageSize());
			return this.ordersHistoryDao.findCondList(condMap);
		}
		/* 不分页查询数据 */
		return this.ordersHistoryDao.findCondList(condMap);
	}
}
